package cz.results.winter6.test;

import cz.results.winter6.io.*;
import cz.results.winter6.model.*;
import cz.results.winter6.oserver.*;
import java.util.Iterator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class BrowseDB extends JFrame {
    
    protected JTable table;
    private TableValuesDB tv = new TableValuesDB();
    
    public static void main(String[] args) {
        BrowseDB browse = new BrowseDB();
        browse.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        browse.setSize(500, 600);
        browse.setVisible(true);
        DBClient.dbCloseClient();
    }
    
    public BrowseDB() {
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
//        TableValuesDB tv = new TableValuesDB();
        table = new JTable(tv);
        JScrollPane jsp = new JScrollPane(table);
        pane.add(jsp, BorderLayout.CENTER);
        
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        pane.add(bottomPanel, BorderLayout.SOUTH);
        
        JButton btnRefresh = new JButton("refresh");
        btnRefresh.addActionListener(new refreshTable());
        
        JButton btnDelete = new JButton("delete");
        btnDelete.addActionListener(new deleteStartSubjekt());
        bottomPanel.add(btnDelete);
        
        
/*
        btnRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("refreshing...");
                tv.refreshTableValuesDB();
                table.updateUI();
            }
        }
        );
 */
        bottomPanel.add(btnRefresh);
        
        
        JButton btnZap = new JButton("zap");
        btnZap.addActionListener(new zapStartList());
        bottomPanel.add(btnZap);
        
        JButton btnImport = new JButton("import");
        btnImport.addActionListener(new importStartList());
        bottomPanel.add(btnImport);
        
    }
    
    public void refreshTable() {
        tv.refreshTableValuesDB();
        table.updateUI();
    }
    
    class deleteStartSubjekt implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println(table.getSelectedRow());
            try {
//                System.out.println(tv.getSSbyIndex(table.getSelectedRow()).toXML());
                DBClient.dbDelete(tv.getSSbyIndex(table.getSelectedRow()));
            } catch (Exception ex) {
                System.out.println("Chyba");
                ex.printStackTrace();
            }
            refreshTable();
        }
    }
    
    class zapStartList implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
                DBClient.dbZap(StartSubjekt.class);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            refreshTable();
        }
    }
    
    class importStartList implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            
            StartSubjektList sl = ImportFromXML.doImportFromXML("/home/winter6/data.xml");
            
            for (Iterator i = sl.iterator(); i.hasNext();) {
                StartSubjekt ss = (StartSubjekt) i.next();
                try {
                    DBClient.dbStore(ss);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                
            }
            refreshTable();
        }
    }
    
    class refreshTable implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            tv.refreshTableValuesDB();
            table.updateUI();
            
        }
    }
}
